package com.test2;

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;

import java.io.File;
import java.nio.file.Paths;
import java.util.Map;

public class ScriptTestFirefox {

    public static void main(String[] args) {
        String path = "C:\\Program Files\\Mozilla Firefox\\geckodriver.exe";
        if(args.length > 0){
            path = args[0];
        }
        get("http://www.baidu.com/",path);
    }

    public static Map<String, String> get(String url,String path) {
        // 参数配置
        System.setProperty("webdriver.gecko.driver", path);

        FirefoxOptions option = new FirefoxOptions();
        option.setBinary("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
        FirefoxProfile firefoxProfile = new FirefoxProfile();
        //firefoxProfile.addExtension(new File("C:\Users\lqmkk\Downloads\at-plugin-master\source"));
        firefoxProfile.setPreference("security.fileuri.strict_origin_policy", false);
        option.setProfile(firefoxProfile);
        FirefoxDriver driver = new FirefoxDriver(option);
        //driver.installExtension(Paths.get("C:\\Users\\lqmkk\\Desktop\\build\\build.zip"),true);

//        driver.manage().window().maximize();
//        driver.manage().deleteAllCookies();
//        driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
//        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        // 创建EventFiringWebDriver 实例
        // Register the Listener with the event firing driver
        driver.get(url);

        try{
            String code = "var s=document.createElement('script'); s.type='text/javascript'; s.src='http://code.jquery.com/jquery-3.6.0.min.js'; document.body.appendChild(s);";
            driver.executeScript(code);
            }catch (Exception e){
            System.out.println("error!!!2");
            }

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try{
            driver.executeAsyncScript("alert($.fn.jquery);  ");
        }catch (Exception e){
            System.out.println("error!!!");
        }


        return null;
    }
}

